03. Annotations
Annotations
In this section, you will:
- Learn what Java annotations are.
- Practice using common built-in Java annotations.
ND079 JPND C2 L04 A03 Annotations
What are Annotations?
Annotations are a way to provide extra metadata about your program. Annotations only provide metadata — they have no other effect on execution of the annotated code.
What Can Annotations Do?
Annotations can add extra information for the Java compiler, which helps the compiler detect additional errors at compile-time.
Annotation metadata can be retained at runtime, so that tools can can discover them using reflection and act on use the annotation metadata to make decisions or perform actions.
Annotations can add compile-time information for code generation tools or other tools that plug into the Java compiler.
Where Can Annotations Be Used?
You can add annotations almost anywhere in your Java program! You can add them to classes, interfaces, fields, methods, constructors, and even type declarations and type parameters.
QUIZ QUESTION::
Match each annotation with its description.
ANSWER CHOICES:
Description |
Annotation |
---|---|
Generates a compiler error if the annotated method does not exist in a superclass. |
|
Generates a compiler warning whenever the annotated method is called. |
|
Generates a compiler error if the interface with this annotation is not a valid functional interface. |
|
Stops the compiler from generating certain warnings. |
SOLUTION:
Description |
Annotation |
---|---|
Generates a compiler error if the interface with this annotation is not a valid functional interface. |
|
Stops the compiler from generating certain warnings. |
|
Generates a compiler error if the annotated method does not exist in a superclass. |
|
Generates a compiler warning whenever the annotated method is called. |
Common Annotations Demo
ND079 JPND C2 L04 A04 Demo Common Annotations
Code from the Demo
In the demo, the @Override
annotation helped the Java compiler identify a bug at compile-time that was otherwise pretty tricky to find at runtime.
With the Bug
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
public final class CartItem {
private final int id;
private final String name;
private double cost;
private CartItem(int id, String name, double cost) {
this.id = id;
this.name = Objects.requireNonNull(name);
this.cost = cost;
}
public boolean equals(CartItem other) {
if (this == other) {
return true;
}
return this.id == other.id && this.name.equals(other.name) && this.cost == other.cost;
}
@Override
public int hashCode() {
return Objects.hash(id, name, cost);
}
public String getName() {
return name;
}
public double getCost() {
return cost;
}
public static void main(String[] args) {
CartItem book1 = new CartItem(17, "Hitchhiker's Guide to the Galaxy", 7.14);
CartItem book2 = new CartItem(17, "Hitchhiker's Guide to the Galaxy", 7.14);
Set<CartItem> items = new HashSet<>();
items.add(book1);
items.add(book2);
System.out.println(items.size());
}
}
Without the Bug
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
public final class CartItem {
private final int id;
private final String name;
private double cost;
private CartItem(int id, String name, double cost) {
this.id = id;
this.name = Objects.requireNonNull(name);
this.cost = cost;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof CartItem)) {
return false;
}
CartItem o = (CartItem) other;
return this.id == o.id && this.name.equals(o.name) && this.cost == o.cost;
}
@Override
public int hashCode() {
return Objects.hash(id, name, cost);
}
public String getName() {
return name;
}
public double getCost() {
return cost;
}
public static void main(String[] args) {
CartItem book1 = new CartItem(17, "Hitchhiker's Guide to the Galaxy", 7.14);
CartItem book2 = new CartItem(17, "Hitchhiker's Guide to the Galaxy", 7.14);
Set<CartItem> items = new HashSet<>();
items.add(book1);
items.add(book2);
System.out.println(items.size());
}
}